<?php
namespace app\model;

use app\common\model\_Base;

class Log extends _Base
{
    /**
     * 设置关联模型Permission
     */
    public function permission()
    {
        return $this->hasOne('Permission','path','path');
    }

    /**
     * 保存数据
     * @param  obj $data 数据
     * @param  integer  $id 数据编号
     * @return bool       
     */
    public static function doCreate($data)
    {   
        $log = new Log;
        $error = $log->allowField(true)->save($data);
        return;
    }

    /**
     * 获取数据
     * @param  array    $cond  条件
     * @param  string   $alias 条件表别名
     * @param  integer  $page  页码
     * @param  integer  $limit 每页条数
     * @return array
     */
    public static function doList($cond = [], $page = 1, $size = 10, $order = 'id desc')
    {   
        $total = Log::where($cond)->count();

        $list = Log::with('permission')->where($cond)->page($page,$size)->order($order)->select();
        if(!$list->isEmpty()){
            foreach ($list as $key => &$value) {
                $name = $value->permission['name'];
                unset($value->permission);
                $value->permission = $name;
                $value->user_type = $value->user_type == Log::TYPE_ADMIN ? '管理员' : '会员';
                $value->data = $value->data?unserialize($value->data):'';
            }
        }
        return ['total'=>$total,'list' => $list];
    }
}